package com.start.form.engine.core.tools;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.start.common.exceptions.StartBusException;
import com.start.form.engine.core.entity.StartFormConfig;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class StartTableOptionTools {
    /**
     * 获取列信息
     *
     * @param config
     * @return
     */
    public static JSONArray getColumn(StartFormConfig config) {
        return getColumn(config.getFormOption());
    }

    public static JSONArray getColumn(JSONObject tableOption) {
        if (tableOption == null || tableOption.isEmpty()) {
            throw new StartBusException("数据表配置不能为空");
        }
        if (!tableOption.containsKey("column")) {
            throw new StartBusException("该数据表未找到列配置");
        }
        JSONArray column = tableOption.getJSONArray("column");
        if (column == null || column.isEmpty()) {
            throw new StartBusException("该数据表未找到列配置");
        }
        return column;
    }

    /**
     * 获取字段索引
     *
     * @param formConfig
     * @return
     */
    public static Map<String, Integer> getTableOptionFieldIndex(StartFormConfig formConfig) {
        JSONArray column = getColumn(formConfig);
        return getTableOptionFieldIndex(column);

    }

    /**
     * 获取字段索引
     *
     * @param column
     * @return
     */
    public static Map<String, Integer> getTableOptionFieldIndex(JSONArray column) {
        Map<String, Integer> colFields = new HashMap<>();
        for (int i = 0; i < column.size(); i++) {
            JSONObject col = column.getJSONObject(i);
            if (!col.containsKey("prop") || StringUtils.isEmpty(col.getString("prop"))) {
                throw new StartBusException("请设置第" + (i + 1) + "组件的字段名");
            }
            String prop = col.getString("prop");
            if (colFields.containsKey(prop)) {
                throw new StartBusException("第" + colFields.get(prop) + "组件字段名称与第" + (i + 1) + "字段名重复");
            }
            colFields.put(prop, (i + 1));
        }
        return colFields;
    }

    /**
     * 获取字段索引
     *
     * @param formConfig 数据表配置
     * @return
     */
    public static Map<String, JSONObject> getTableColumnMap(StartFormConfig formConfig) {
        JSONArray column = getColumn(formConfig);
        return getTableColumnMap(column);
    }

    /**
     * 获取字段索引
     *
     * @param tableOption 数据表配置
     * @return
     */
    public static Map<String, JSONObject> getTableColumnMap(JSONObject tableOption) {
        JSONArray column = getColumn(tableOption);
        return getTableColumnMap(column);
    }

    /**
     * 获取字段索引
     *
     * @param column
     * @return
     */
    public static Map<String, JSONObject> getTableColumnMap(JSONArray column) {
        Map<String, JSONObject> colFields = new HashMap<>();
        for (int i = 0; i < column.size(); i++) {
            JSONObject col = column.getJSONObject(i);
            if (!col.containsKey("prop") || StringUtils.isEmpty(col.getString("prop"))) {
                throw new StartBusException("请设置第" + (i + 1) + "组件的字段名");
            }
            String prop = col.getString("prop");
            if (colFields.containsKey(prop)) {
                throw new StartBusException("第" + colFields.get(prop) + "组件字段名称与第" + (i + 1) + "字段名重复");
            }
            colFields.put(prop, col);
        }
        return colFields;
    }

}
